package com.zte.warehouse.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zte.util.PageModel;
import com.zte.warehouse.dao.IWarehouseDao;
import com.zte.warehouse.dao.IstockDao;
import com.zte.warehouse.model.*;
import com.zte.warehouse.service.IStockService;
import com.zte.warehouse.service.IWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StockService implements IStockService {

    @Autowired
    private IstockDao istockDao;

    //    分页查询
    @Override
    public PageModel<Stock> queryStockPageByCondition(PageModel<Stock> pageModel, StockCondition conn) {
        int pageNum = pageModel.getPageNum() <= 0 ? 1 : pageModel.getPageNum();
        int pageSize = pageModel.getPageSize() <= 0 ? 6 : pageModel.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<Stock> stocks = istockDao.queryStockListByCondition(conn);
        PageInfo<Stock> info = new PageInfo<Stock>(stocks);

        pageModel.setPageNum(info.getPageNum());
        pageModel.setPageSize(info.getPageSize());
        pageModel.setTotalPage(info.getPages());
        pageModel.setTotalCount((int) info.getTotal());
        pageModel.setData(new ArrayList<Stock>(info.getList()));
        return pageModel;
    }
}
